package com.bw.lyc.mvp.model.db;

import android.content.Context;

public class DBManager {

    private Context context;
    private DaoMaster daoMaster;
    private DaoSession daoSession;

    public void init(Context context) {
        this.context = context;
    }

    public DaoMaster getDaoMaster() throws Exception {
        if (context == null) {
            throw new Exception("context is null");
        }

        if (daoMaster == null) {
            DaoMaster.DevOpenHelper usr = new DaoMaster.DevOpenHelper(context, "usr");
            daoMaster = new DaoMaster(usr.getWritableDatabase());
        }

        return daoMaster;
    }

    public DaoSession getDaoSession() throws Exception {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }

    private static DBManager dbManager;

    public static DBManager getInstance() {
        if (dbManager == null) {
            dbManager = new DBManager();
        }
        return dbManager;
    }
}
